<?php
/**
 * @author yuanyitian
 * @date 2024/11/25 01:00
 * 自动获取前一天的菜单数据并存储，以赋予评价自动取消超时未支付订单
 */
header("content-type:text/html;charset=utf8");
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
$time=time();
$date=date('Y-m-d');

$con = mysqli_connect('127.0.0.1:3306', 'www_skmymz_com', 'zC4tEAxCxkhbMKkp', 'luoping');
mysqli_query($con, "set names 'utf8'");
$res=mysqli_query($con,"select * from menu where `date` like '%$date%' and status=1 and is_delete=1");
$res=mysqli_fetch_all($res,true);
if(count($res)!=0){
    $menu=$res[0];
    $menu_id=(int)$menu['id'];
    $menu_name=$menu['name'];

    $goods=mysqli_query($con,"select a.type,b.* from menu_goods_config a left join goods b on(a.goods_id=b.id and b.status=1 and b.is_delete=1) where a.status=1 and a.is_delete=1 and a.menu_id=$menu_id");
    $goods=mysqli_fetch_all($goods,true);

    if(count($goods)==0){
        mysqli_close($con);
        echo "菜单：$menu_name($menu_id)在$date 无商品";
        return ;
    }

    $sql=[];
    for($i=0;$i<count($goods);$i++){
        $type=(int)$goods[$i]['type'];
        $goods_id=(int)$goods[$i]['id'];
        $goods_name=$goods[$i]['name'];
        $url=$goods[$i]['img_url'];
        $sql[]="($menu_id,'$menu_name',$type,$goods_id,'$goods_name','$url',$time,$time)";
    }
    $sql=implode(',',$sql);
    $res=mysqli_query($con,"insert into menu_day_record(menu_id,menu_name,type,goods_id,goods_name,goods_url,create_time,update_time) values$sql");
    mysqli_close($con);
    if($res){
        echo '每日菜单数据记录成功！';
    }else{
        echo "每日菜单数据记录失败！其sql语句为：insert into menu_day_record(menu_id,menu_name,type,goods_id,goods_name,goods_url,create_time,update_time) values$sql";
    }

}else{
    mysqli_close($con);
    echo '今日无菜单！';
}


